clear
u ${temp}\temp2
/* drop if jbstatt<0  */

/* recodes starting and ending information */
recode jhendd -1=1 -9=. -2=.
recode jhbgd -1=1 -9=. -2=.
recode jhendm -1=7 -9=. -2=. 13=1 14=4 15=7 16=10
recode jhbgm -1=7 -9=. -2=. 13=1 14=4 15=7 16=10
recode jhendy -1=. -9=.
recode jhbgy -1=. -9=.
recode jhstp -9/-1=.
recode jhstat -9/-1=.
recode jblky -9/-1=.
/** starting date for spell ***/
replace jhbgy4=jhbgy+1900 if jhbgy4==.
replace jhendy4=jhendy+1900 if jhendy4==.
g jhbg=mdy(jhbgm,jhbgd,jhbgy4)/365

/** ending date for spell ***/
g jhend=mdy(jhendm,jhendd,jhendy4)/365


egen maxjsp=max(jspno), by(hid pno)
drop if maxjsp==.
replace jhbg=-99 if jhbg==.&jspno==maxjsp

g sept1=mdy(9,1,wave+1990)/365

/*** this procedure works out what has happened between idatel and idate ***/
g rec=jhend>=idatel&jhend~=. if idatel~=.
g lastintj=jhend>=idatel&jhend~=.&jhbg<=idatel if idatel~=.
g jspno_li=jspno if lastintj==1
g jhend_li=jhend if lastintj==1
g jhbeg_li=jhbg if lastintj==1
g jhstat_li=jhstat if lastintj==1
sort hid pno jspno
quie by hid pno: g chk=jhbg-jhend[_n+1] if rec==1
g length=max(jhend,idatel)-max(jhbg,idatel) if jhend~=.&jhbg~=.&idatel~=.
replace length=0 if length<0
g emp=jhstat<3
g unemp=jhstat==3
g inac=jhstat>3&jhstat~=.&jhstat~=5
g samemp=jhstat==1
g zmatleav=jhstat==5 if rec==1
egen nsame=sum(samemp*rec) , by(hid pno)  /* no. of moves w/same emps */
egen nspells=sum(rec), by(hid pno)
egen jspells=sum(emp*rec), by(hid pno)
egen jbweeks1=sum(length*emp), by(hid pno)
egen uspells=sum(unemp*rec), by(hid pno)
egen juweeks1=sum(length*unemp), by(hid pno)
egen ispells=sum(inac*rec), by(hid pno)
egen jiweeks1=sum(length*inac), by(hid pno)
egen mspells=sum(zmatleav*rec), by(hid pno)
egen jmweeks1=sum(length*zmatleav), by(hid pno)

/*** this procedure tries to compute tenure of current job  ***/
egen early=min(jspno) if jhstat~=1&rec==1, by(hid pno) /* most recent job after idate(-1) 
             which is not with the same employer */ 
g beg=jhend if early==jspno
egen ltz=count(early), by(hid pno)
g lt=ltz==0
g jhrec=1

/* works out what was being done immediately before start of new employer */
g prnempz=jhstat>2 if early==jspno&jhstat~=.
egen prevne=max(prnempz) if lt==0, by(hid pno)

/** this works out status at previous idate **/
g prevstatz=jhstat if (jhbg<=idatel|jhbg==.)&jhend>idatel&jhend~=.
g prevstat=prevstatz
recode prevstat -9/-1=. 1/2=1 3/10=0

g recspell=jhstat==1 if jspno==1
egen maxend=max(jhend), by(hid pno)
egen minbg=min(jhbg), by(hid pno)
g totlen=max(maxend,idatel)-max(minbg,idatel) if minbg~=.&maxend~=.*idatel~=.
egen totl2=sum(length), by(hid pno)

/* this splits better job into wage and non-wage reasons */
g x=1 if jhstp==1&jhstat==2
replace jhstp=2 if jhstp==1&jhstat==2   /* takes promotions into better job */
replace jhstp=1 if jhstp==2&jhstat==2&jblky>3&x~=1
/* this works out reasons for job stopping */
local i=1 
while `i'<=13 {
egen yends`i'=sum(jhstp==`i'&jhstat==1&rec==1), by(hid pno)
egen yendm`i'=sum(jhstp==`i'&jhstat==2&rec==1), by(hid pno)
local i=`i'+1
}
compress yend*

/* this works out reason for job at previous interview stopping */
egen ljspno=max(jspno) if jhend>idatel&jhend~=.&jhstat~=1, by(hid pno)
g ljyend=jhstp if jspno==ljspno

g beg_h=.
g end_h=.
g begy_h=.
g endy_h=.
g payn_h=.
g payg_h=.
g sic_h=.
g sic92_h=.
g size_h=.

/*this works out beg, end, pay and industry of most recent job*/

sort hid pno
egen minjspno=min(jspno), by(hid pno)
replace begy_h=jhbgy4 if jspno==minjspno
replace endy_h=jhendy4 if jspno==minjspno
replace beg_h=jhbg if jspno==minjspno
replace end_h=jhend if jspno==minjspno
replace payn_h=jhnpay if jspno==minjspno
replace payg_h=jhgpay if jspno==minjspno
replace sic_h=jhsic if jspno==minjspno
replace sic92_h=jhsic if jspno==minjspno
replace size_h=jhsize if jspno==minjspno

collapse beg_h end_h begy_h endy_h payn_h payg_h sic_h sic92_h size_h minjspno nsame nspells jspells jbweeks1 uspells juweeks1 ispells jiweeks1 mspells jmweeks1 jspno_li jhend_li yend* jhbeg_li jhbg jhend lastintj /*
*/ beg lt prevne jhrec prevstat* idatel recspell totlen maxend totl2 ljyend ljspno jhstat_li, by(hid pno)
sort hid pno
save ${temp}\temp2,replace
merge hid pno using ${temp}\temp1
drop _merge



